package com.catsic.security.x509;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.catsic.base.exception.CatsicException;
import com.catsic.core.user.service.UserService;

@Service("securityAuthenticationUserDetailsService")
public class SecurityAuthenticationUserDetailsService implements
		AuthenticationUserDetailsService<Authentication> {
	
	@Autowired
	private UserService userService;
	
	@Override
	public UserDetails loadUserDetails(Authentication token)
			throws UsernameNotFoundException {
		try{
			return userService.loadUserByUsername(token.getName());
		}catch(CatsicException e){
			throw new UsernameNotFoundException(e.getMessage());
		}
	}
}
